# -*- coding: utf-8 -*-
import apiclient
import json

class APIMessage(object):
   STORE_DATA=0
   def __init__(self,adata):
      d=json.loads(unicode(adata,'utf8','replace'))
      if self.STORE_DATA:
         self.data=adata
         self.d=d
      self.status=d['status']
      self.info=d.get('info','')
      self.cnt=d.get('cnt',0)
      self.type=d.get('type','')
      self.paraminfo=d.get('paraminfo',{})
      self.items=d.get('items',[])
   def dump(self):
      print 'status:',self.status
      print 'info:',self.info
      print 'cnt:',self.cnt
      print 'type:',self.type
      print 'items:'
      for i in range(len(self.items)):
         print '  item %d:'%(i,)
         d=self.items[i]
         for k,v in d.items():
            print '    %s = %s'%(`k`,`v`)
      print 'paraminfo:'
      for k,v in self.paraminfo.items():
         print '  %s = %s'%(k,v)
   def dumpJSON(self):
      if self.STORE_DATA:
         print json.dumps(self.d,indent=4)

class CmentarzeAPI(apiclient.APIClient):
   BASE_URL='http://cmentarze.szczecin.pl/cmentarze/api/'
   def _handle_response(self,response):
      amsg=APIMessage(response.data)
      return amsg
   def person_GetList(self,fsurname='',fname='',fdatebirth='',fdatecease=''):
      return self._request('GET','person_GetList.asp',{'fsurname':fsurname,'fname':fname,'fdatebirth':fdatebirth,'fdatecease':fdatecease})
   def person_GetDetails(self,personid):
      return self._request('GET','person_GetDetails.asp',{'personid':personid})
   def grave_GetDetails(self,graveid):
      return self._request('GET','grave_GetDetails.asp',{'graveid':graveid})
   def map_GetViewByPerson(self,personid):
      return self._request('GET','map_GetViewByPerson.asp',{'personid':personid})
   def map_GetViewByGrave(self,graveid):
      return self._request('GET','map_GetViewByGrave.asp',{'graveid':graveid})
   def map_GetViewByGrave(self,graveid):
      return self._request('GET','map_GetViewByGrave.asp',{'graveid':graveid})
   def map_GetPosByLatLon(self,lat,lon,angle,ext,width,height):
      return self._request('GET','map_GetPosByLatLon.asp',{'lat':lat,'lon':lon,'angle':angle,'ext':ext,'width':width,'height':height})

if __name__=='__main__':
   APIMessage.STORE_DATA=1
   aAPI=CmentarzeAPI()
   amsg=aAPI.person_GetList()
   amsg.dumpJSON()
   amsg=aAPI.person_GetDetails('D1AB66FD7E334760B2F31B1744D97771')
   amsg.dumpJSON()
